clear
clear matrix
set more off

* ------------------------------------------------------------------------------
* set paths
global root "[include path here]"
global input "$root/Input"
global output "$root/Output"
global intermediate "$root/Intermediate"
global figures "$root/Figures"

* ______________________________________________________________________________
* FULL PANEL REGRESSIONS
use "$input/eventStudyPanel_raw.dta", clear
keep if conversions == 0 | (conversions == 1 & wg == "SF")

gen log_wuse = log(wuse)

* merge application information into panel
sort pid year month
merge m:1 pid using "$input/applications.dta"
sort pid year month
drop if _merge == 2
tab _merge conversions

* generate pre-period indicator
gen pre = 0
replace pre = 1 if ((year == year(appdate) & month >= month(appdate)) | ///
(year > year(appdate))) & tau < 0

* generate proxy application and enrollment dates
local proxy = 5
gen proxy_tau = 12*(year-event_year + `proxy') + month - event_month ///
	if conversions == 1
gen proxy_appdate = appdate - 365*`proxy'
format proxy_appdate %tdnn/dd/CCYY

gen proxy_chi = 0
replace proxy_chi = 1 if proxy_tau >= 0 & proxy_tau !=.

gen proxy_pre = 0
replace proxy_pre = 1 if ((year == year(proxy_appdate) ///
	& month >= month(proxy_appdate)) ///
	| (year > year(proxy_appdate))) & proxy_tau < 0

* generate zero consumption indicator
by pid: egen minwuse = min(wuse)
gen zero = 0
replace zero = 1 if minwuse == 0
la var zero "1 if parcel contains zero water use anywhere"

tab conversions, missing
tab conversions if zero == 0, missing
tab conversions zero, missing
tab balanced conversions if zero == 0, missing
tab cohort2 conversions if zero==0, missing

* define local fixed effect specification variables
local fe_pFEymFE   "i.pid i.year#i.month"
local fe_pFEymc2FE "i.pid i.year#i.month#i.cohort2"
local fe_pmFEymFE   "i.pid#i.month i.year#i.month"
local fe_pmFEymc2FE "i.pid#i.month i.year#i.month#i.cohort2"

local fixedeffect "pmFEymc2FE" // "pFEymFE pFEymc2FE pmFEymFE pmFEymc2FE"
local depvar "wuse log_wuse"
foreach dv of local depvar {
foreach fe of local fixedeffect {

display in red "******* main specification: dep var is `dv'"
reghdfe `dv' chi pre, a(`fe_`fe'') pool(1) vce(cl pid)
	local regid "`dv'_`fe'"
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`regid'.ster", replace
	
display in red "******* main specification without controlling for pre-period:"
display in red "	dep var is `dv'"
reghdfe `dv' chi, a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'noPre.ster", replace

display in red "******* Falsification test: dep var is `dv'"
reghdfe `dv' proxy_chi proxy_pre if conversions == 0 ///
	| (conversions == 1 & tau < -24), a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'proxy.ster", replace
}
* define preferred fixed-effect specification for remaining regressions
local fe "pmFEymc2FE"

foreach yr of numlist 2004/2009 {
display in red "******* limit observations through `yr': dep var is `dv'"
reghdfe `dv' chi pre if year <= `yr', a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	local regid "`dv'_`fe'le`yr'"
	estimates save "$output/did_`regid'.ster", replace
}

display in red "******* 1-year donut: dep var is `dv'"
reghdfe `dv' chi pre if (tau<-12 | tau>12), a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'1yrD.ster", replace

display in red "******* 2-year donut: dep var is `dv'"
reghdfe `dv' chi pre if (tau<-24 | tau>24), a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'2yrD.ster", replace

display in red "******* balanced sample: dep var is `dv'"
reghdfe `dv' chi pre if balanced==1, a(`fe_pmFEymFE') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'baltru.ster", replace
	
display in red "******* no zero: dep var is `dv'"
reghdfe `dv' chi pre if zero == 0, a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'noZero.ster", replace

display in red "******* chi-by-tau: dep var is `dv'"
reghdfe `dv' chi pre chiBYtau, a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'tau.ster", replace

display in red "******* chi-by-tau^2: dep var is `dv'"
reghdfe `dv' chi pre chiBYtau chiBYtau2, a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'tau2.ster", replace	

display in red "******* no early exits: dep var is `dv'"
reghdfe `dv' chi pre if earlyx==0, a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'ex0.ster", replace

display in red "****** participants only: dep var is `dv'"
reghdfe `dv' chi pre if conversions == 1, a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'onlyEnroll.ster", replace

display in red "****** chi-by-minimum conversion: dep var is `dv'"
reghdfe `dv' chi pre chiBYminconv, a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'minconv.ster", replace

display in red "****** chi-by-(price-per-square-foot): dep var is `dv'"
reghdfe `dv' chi pre chiBYppsf, a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/did_`dv'_`fe'ppsf.ster", replace
}

* ______________________________________________________________________________
* MATCHED PANEL REGRESSIONS
use "$input/eventStudyPanel_matched.dta", clear

gen log_wuse = log(wuse)

* generate proxy application and enrollment dates
local proxy = 5
gen proxy_tau = 12*(year-event_year + `proxy') + month - event_month ///
	if conversions == 1
gen proxy_appdate = appdate - 365*`proxy'
format proxy_appdate %tdnn/dd/CCYY

gen proxy_chi = 0
replace proxy_chi = 1 if proxy_tau >= 0 & proxy_tau !=.

gen proxy_pre = 0
replace proxy_pre = 1 if ((year == year(proxy_appdate) ///
	& month >= month(proxy_appdate)) ///
	| (year > year(proxy_appdate))) & proxy_tau < 0

* define local fixed effect specification variables
local fe_pFEymFE   "i.pid i.year#i.month"
local fe_pmFEymFE   "i.pid#i.month i.year#i.month"

local fixedeffect "pmFEymFE" // "pFEymFE pmFEymFE"
local depvar "wuse log_wuse"
foreach dv of local depvar {
foreach fe of local fixedeffect {
display in red "******* main specification: dep var is `dv'"
reghdfe `dv' chi pre [fweight=parcelfreq], a(`fe_`fe'') pool(1) vce(cl pid)
	local regid "`dv'_`fe'"
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`regid'.ster", replace
		
display in red "******* main specification without controlling for pre-period:"
display in red "	dep var is `dv'"
reghdfe `dv' chi [fweight=parcelfreq], a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`dv'_`fe'noPre.ster", replace

display in red "******* Falsification test: dep var is `dv'"
reghdfe `dv' proxy_chi proxy_pre if conversions == 0 ///
	| (conversions == 1 & tau < -24) [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`dv'_`fe'proxy.ster", replace
}
* define preferred fixed-effect specification for remaining regressions
local fe "pmFEymFE"

foreach yr of numlist 2004/2009 {
display in red "******* limit observations through `yr': dep var is `dv'"
reghdfe `dv' chi pre if year <= `yr' [fweight=parcelfreq], a(`fe_`fe'') ///
pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	local regid "`dv'_`fe'le`yr'"
	estimates save "$output/didmatch_`regid'.ster", replace
}

display in red "******* 1-year donut: dep var is `dv'"
reghdfe `dv' chi pre if (tau<-12 | tau>12) [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`dv'_`fe'1yrD.ster", replace

display in red "******* 2-year donut: dep var is `dv'"
reghdfe `dv' chi pre if (tau<-24 | tau>24) [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`dv'_`fe'2yrD.ster", replace

display in red "******* no zero: dep var is `dv'"
reghdfe `dv' chi pre if zero == 0 [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`dv'_`fe'noZero.ster", replace

display in red "******* chi-by-tau: dep var is `dv'"
reghdfe `dv' chi pre chiBYtau [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`dv'_`fe'tau.ster", replace

display in red "******* chi-by-tau^2: dep var is `dv'"
reghdfe `dv' chi pre chiBYtau chiBYtau2 [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`dv'_`fe'tau2.ster", replace	

display in red "****** chi-by-minimum conversion: dep var is `dv'"
reghdfe `dv' chi pre chiBYminconv [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`dv'_`fe'minconv.ster", replace

display in red "****** chi-by-(price-per-square-foot): dep var is `dv'"
reghdfe `dv' chi pre chiBYppsf [fweight=parcelfreq], ///
	a(`fe_`fe'') pool(1) vce(cl pid)
	display "command line: `e(cmdline)'"
	estimates save "$output/didmatch_`dv'_`fe'ppsf.ster", replace
}